package com.disruptor.demo;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * 主函数入口
 *
 * @创建者: 宋晓虎
 * @创建时间: 2025/11/2  19:40
 */
public class Main {
    public static void main(String[] args) {
        //创建订单事件工厂
        OrderEventFactory orderEventFactory = new OrderEventFactory();
        //设置内存队列容量，size
        int ringBufferSize = 1024 * 1024;
        //线程池工厂
        ThreadFactory threadFactory = Executors.defaultThreadFactory();

        Disruptor disruptor = new Disruptor(
                orderEventFactory,
                ringBufferSize,
                threadFactory,
                ProducerType.SINGLE,
                new BlockingWaitStrategy()
        );
        //设置消费者处理器
        disruptor.handleEventsWith(new OrderEventHandler());
        disruptor.start();

        //获取数据容器
        RingBuffer ringBuffer = disruptor.getRingBuffer();
        //构建生产者
        OrderEventProducer orderEventProducer=new OrderEventProducer(ringBuffer);
        //模拟发送数据
        for (int i = 0; i < 100000; i++) {
            orderEventProducer.sendData(System.currentTimeMillis());
        }

        //所有事件处理完成后关闭
        disruptor.shutdown();

        //立即关闭
        //disruptor.halt();
    }
}

